﻿using System;
using System.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using POS.BusinessEntities;
using POS.DataObjects;

namespace POS.DataObjectsTest
{   
    [TestClass]
    public class DOArticuloTest
    {
        #region Declarations
        private static Database db;
        //private const string TestUser = "testUser";
        private const int EmpresaId = 1;

        #endregion
        
        #region Properties: Non-Public

        private static Database Db
        {
            get
            {
                if (db == null)
                    db = DatabaseFactory.CreateDatabase();
                return db;
            }
        }

        #endregion
        
        #region Test Methods

        [TestMethod]
        public void GetListArticuloSinFiltro()
        {
            var userName = DateTime.Now.Ticks.ToString();
            CreateArticulos(userName);

            var ds = Db.ExecuteDataSet(CommandType.Text, "select * from articulo where empresaId = " + EmpresaId);
            var filtro = string.Empty;

            var results = new DOArticulo().GetList(EmpresaId, filtro, SearchField.Name);
            Helper.AssertGetListResult(ds, results);

            DeleteArticulos(userName);
        }

        //[TestMethod]
        //public void SaveImpuestoNew()
        //{
        //    const string name = "TestName";
        //    const decimal percentage = (decimal) 15.5;

        //    var entity = new BEImpuesto { Nombre = name, Porcentaje = percentage };

        //    Db.ExecuteNonQuery(CommandType.Text, "delete impuesto where empresaId = " + EmpresaId);
        //    new DOImpuesto().Save(entity, EmpresaId, TestUser);
        //    var ds = Db.ExecuteDataSet(CommandType.Text, "select * from impuesto where empresaId=" + EmpresaId);
            
        //    Assert.IsNotNull(ds);
        //    Assert.AreEqual(1, ds.Tables.Count);
        //    Assert.AreEqual(1, ds.Tables[0].Rows.Count);
        //    var row = ds.Tables[0].Rows[0];
        //    Assert.IsTrue((int)row["ImpuestoId"] > 0);
        //    Assert.AreEqual(name, row["Nombre"]);
        //    Assert.AreEqual(percentage, row["Porcentaje"]);
        //    Assert.AreEqual(true, row["Activo"]);
        //    Assert.AreEqual(TestUser, row["ModificadoPor"]);
        //    Assert.AreEqual(DateTime.Today.ToShortDateString(), DateTime.Parse(row["FechaModificacion"].ToString()).ToShortDateString());
        //    Assert.AreEqual(DateTime.Today.ToShortDateString(), DateTime.Parse(row["FechaCreacion"].ToString()).ToShortDateString());
        //}

        //[TestMethod]
        //public void SaveImpuestoExisting()
        //{
        //    const string name = "TestName";
        //    const decimal percentage = (decimal)15.5;

        //    Db.ExecuteNonQuery(CommandType.Text, "delete impuesto where empresaId = " + EmpresaId);
        //    var id = CreateImpuesto(name);
        //    var expectedName = name + DateTime.Now.Ticks;
        //    var expectedUser = TestUser + DateTime.Now.Ticks;
        //    var entity = new BEImpuesto { Id = id, Nombre = expectedName, Porcentaje = percentage };

        //    new DOImpuesto().Save(entity, EmpresaId, expectedUser);

        //    var ds = Db.ExecuteDataSet(CommandType.Text, "select * from Impuesto where empresaId=" + EmpresaId);
        //    Assert.IsNotNull(ds);
        //    Assert.AreEqual(1, ds.Tables.Count);
        //    Assert.AreEqual(1, ds.Tables[0].Rows.Count);
        //    var row = ds.Tables[0].Rows[0];
        //    Assert.IsTrue((int)row["ImpuestoId"] > 0);
        //    Assert.AreEqual(expectedName, row["Nombre"]);
        //    Assert.AreEqual(percentage, row["Porcentaje"]);
        //    Assert.AreEqual(true, row["Activo"]);
        //    Assert.AreEqual(expectedUser, row["ModificadoPor"]);
        //    Assert.AreEqual(DateTime.Today.ToShortDateString(), DateTime.Parse(row["FechaModificacion"].ToString()).ToShortDateString());
        //}


        #endregion

        #region Test Utilities
        
        private static void CreateArticulos(string name)
        {
            for(var i=0; i<10; i++)
            {
                var sql = "INSERT INTO Articulo " +
                          "(ArticuloId,Codigo,Descripcion,DescripcionCorta,ActivoVentas,ActivoCompras,ControlSerie,ControlLote, " +
                          " ControlInventario,Costo,CostoUltimo,FechaCreacion,FechaModificacion,ModificadoPor,EmpresaId) " +
                          "VALUES " +
                          " (" + i + ", 'Codigo " + i + "', 'Descripcion " + i + "', 'Desc " + i + "', 1,1,1,1, " +
                          "  1,100,100,getdate(),getdate(),'" + name + "',1)";
                Db.ExecuteNonQuery(CommandType.Text, sql);
            }
        }

        private static void DeleteArticulos(string name)
        {
            var sql = "Delete articulo where modificadoPor = '" + name + "'";
            Db.ExecuteNonQuery(CommandType.Text, sql);
        }
        #endregion
    }
}
